<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;
class UserController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.user.add');
    }
    // insert
    public function insert(Request $request)
    {
    	$this -> validate($request,[
			'name' => 'required|min:6|max:18',
			'password' => 'required',
			'repassword' => 'required|same:password',
			'email' => 'required|email'
		],[
			'name.required' => '用户名不能为空',
			'name.min' => '用户名不能少于6位',
			'name.max' => '用户名不能大于18位',
			'password.required' => '密码不能为空',
			'repassword.required' => '确认密码不能为空',
			'repassword.same' => '两次不一致',
			'email.email' => '邮箱不合法',
			'email.required' => '邮箱不能为空',
		]);
    	$data = $request -> except('_token','repassword');

    	$data['password'] = Crypt::encrypt($data['password']);
    	// 处理token
    	$data['token']=str_random(50);
    	// 用户状态
    	$data['status'] = 0;
    	// dd($data);
    	// 添加
    	$res = DB::table('manager') -> insert($data);
    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }
    public function index(Request $request)
    {
    	// echo '用户列表';
    	$data = DB::table('manager')
    		 -> where('name','like','%'.$request -> input('search').'%')
    		 -> paginate($request -> input('num',10));
    	// dd($data->render());
    	// foreach ($data as $key => $value) {
    	// 	var_dump($value -> name);
    	// }
    	// die;
    	return view('admin.user.index',['data' => $data,'request' => $request -> all()]);
    }
    public function ajaxUpdate(Request $request)
    {
        // return 111;
        // dd($request -> all());
        $id = $request -> input('id');
        $data = DB::table('manager') -> select('status') -> where('id',$id) -> first();
        // dd($data);
        if($data -> status == 0)
        {
            $res = DB::table('manager') -> where('id',$id) -> update(['status' => 1]);
            if($res)
            {
              return response() -> json(1);  
            }else
            {
              // 4 表示修改失败
              return response() -> json(4);
            }
            
        }else
        {
            $res = DB::table('manager') -> where('id',$id) -> update(['status' => 0]);
            if($res)
            {
              return response() -> json(0);  
            }else
            {
              // 4 表示修改失败
              return response() -> json(4);
            }
        }
        // return $id;
    }
    public function ajaxName(Request $request)
    {
        // dd($request -> all());
        // return response() -> json($request -> all());
        $res = DB::table('manager') -> where('name',$request -> input('name')) -> where('id','<>',$request -> input('id')) -> first();
        if($res)
        {
            return response() -> json(5); // 5 表示用户名已经存在
        }else
        {
            $r = DB::table('manager') -> where('id',$request -> id) -> update(['name' => $request -> name]);
            if($r)
            {
                // 0 代表修改成功
                return response() -> json(0);
            }else
            {
                // 1 代表修改失败
                return response() -> json(1);
            }
        }
    }
    public function edit($id)
    {
        // return $id;
        $data = DB::table('manager') -> where('id',$id) -> first();
        return view('admin.user.edit',['data' => $data]);
    }
    public function update(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        $res = DB::table('manager') -> where('id',$data['id']) -> update($data);
        if($res)
        {
            return redirect('/admin/user/index') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }
    public function delete($id)
    {
        $res = DB::table('manager') -> delete($id);
        if($res)
        {
            return redirect('/admin/user/index') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    }
}
